<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%String ctx = request.getContextPath();%>
<title>create question judge</title>
</head>

<script>

	$(document).ready(function(){
		<c:if test="${fn:length(type) gt 0}">
			$("#title").html("编辑判断题")
			$("#judgecontent").html("${question.content}")
			<c:choose>
				<c:when test="${question.answer eq 'true'}">
					$("#true").prop("checked", true)
					$("#false").prop("checked", false)
				</c:when>
				<c:otherwise>
					$("#true").prop("checked", false)
					$("#false").prop("checked", true)
				</c:otherwise>
			</c:choose>
		</c:if>
	});

	function addQuestionJudge() 
	{
		let content = $("#judgecontent").val();
		if(content.length === 0) {
			alert('请输入题干');
			return;
		}
		
		let radios = document.getElementsByName('ans');
		if(!radios[0].checked && !radios[1].checked) {
			alert('请输入答案');
			return;
		}
		let answer = radios[0].checked;
		$.ajax(
			{
				type:"POST",
				url: "<%=ctx%>/question?act=addQuestion&type=judge",
				data: {
					"content" :content,
					"answer": answer
				},
				success: function(data, status) {
					alert(data);
					AjaxLoad('<%=ctx%>/control?act=createJudge')
				},
			  	error: function(req, status, msg) {
					alert(msg)
			  	}
			}
		)
	}

	function updateQuestionJudge()
	{
		let content = $("#judgecontent").val();
		if(content.length === 0) {
			alert('请输入题干');
			return;
		}

		let radios = document.getElementsByName('ans');
		if(!radios[0].checked && !radios[1].checked) {
			alert('请输入答案');
			return;
		}
		let answer = radios[0].checked;
		$.ajax(
				{
					type:"POST",
					url: "<%=ctx%>/question?act=updateQuestion&type=judge&id="+"${question.id}",
					data: {
						"content" :content,
						"answer": answer
					},
					success: function(data, status) {
						alert(data);
						AjaxLoad('<%=ctx%>/control?act=questionOpera')
					},
					error: function(req, status, msg) {
						alert(msg)
					}
				}
		)
	}

</script>

<body>
	<div id="list" class="col-md-10 col-xs-12">
		<h3 id="title">创建判断题</h3>
		<hr>
    
		<div class="fiter col-md-10 col-xs-12">
   			<textarea class="form-control" id="judgecontent" rows="2" placeholder="请输入题干"></textarea>
			<h5>&nbsp;</h5>
			<input name="ans" type="radio" id="true" value="T">
 				<label for="true">正确</label><br	>
			<input name="ans" type="radio" id="false" value="F">
 				<label for="false">错误</label>
			<h5>&nbsp;</h5>

			<c:choose>
				<c:when test="${fn:length(type) gt 0}">
					<button type="button" class="btn btn-success" onclick="updateQuestionJudge()">确定</button>
				</c:when>
				<c:otherwise>
					<button type="button" class="btn btn-success" onclick="addQuestionJudge()">确定</button>
				</c:otherwise>
			</c:choose>

		</div>
	</div>
</body>
</html>